package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.mapper.FacilityInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private FacilityInfoService facilityInfoService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private LabelInfoService labelInfoService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private FeeValueService feeValueService;
    @Autowired
    private FeeKeyService feeKeyService;

    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;

    @Override
    public IPage<ApartmentItemVo> getApartmentItemVoPage(Page<ApartmentInfo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.getApartmentItemVoPage(page,queryVo);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailVoById(Long id) {
        ApartmentInfo apartmentInfo = this.getById(id);

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);

//        查到中间表放入vo
//        @Schema(description = "图片列表")
//        private List<GraphVo> graphVoList;
        List<GraphVo> graphVoList = graphInfoService.getGraphVoByApartmentId(id);
        apartmentDetailVo.setGraphVoList(graphVoList);
//        @Schema(description = "标签列表")
//        private List<LabelInfo> labelInfoList;
        List<LabelInfo> labelInfoList = labelInfoService.getLableInfoByApartmentId(id);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
//        @Schema(description = "配套列表")
//        private List<FacilityInfo> facilityInfoList;
        List<FacilityInfo> facilityInfoList = facilityInfoService.getFacilityInfoByApartmentId(id);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
//        @Schema(description = "杂费列表")
//        private List<FeeValueVo> feeValueVoList;
        List<FeeValueVo> feeValueVoList = feeValueService.getFeeValueByApartmentId(id);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;
    }

    @Override
    public void updateApartmentInfo(ApartmentSubmitVo apartmentSubmitVo) {

        apartmentSubmitVo.setProvinceName(provinceInfoService.getById(apartmentSubmitVo.getProvinceId()).getName());
        apartmentSubmitVo.setCityName(cityInfoService.getById(apartmentSubmitVo.getCityId()).getName());
        apartmentSubmitVo.setDistrictName(districtInfoService.getById(apartmentSubmitVo.getDistrictId()).getName());

        this.saveOrUpdate(apartmentSubmitVo);

        if(apartmentSubmitVo.getId() != null){
            Long id = apartmentSubmitVo.getId();
//            @Schema(description="公寓配套id")
//            private List<Long> facilityInfoIds;
            apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,id));
//            @Schema(description="公寓标签id")
//            private List<Long> labelIds;
            apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,id));
//            @Schema(description="公寓杂费值id")
//            private List<Long> feeValueIds;
            apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,id));
//            @Schema(description="公寓图片id")
//            private List<GraphVo> graphVoList;
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType,ItemType.APARTMENT).eq(GraphInfo::getItemId,id));
        }

//        重新添加
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
        facilityInfoIds.forEach(facilityInfoId -> {
            ApartmentFacility apartmentFacility =  ApartmentFacility.builder()
                    .apartmentId(apartmentSubmitVo.getId())
                    .facilityId(facilityInfoId)
                    .build();
            apartmentFacilities.add(apartmentFacility);
        });
        apartmentFacilityService.saveBatch(apartmentFacilities);

        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
        labelIds.forEach(labelId -> {
            ApartmentLabel build = ApartmentLabel.builder().apartmentId(apartmentSubmitVo.getId()).labelId(labelId).build();
            apartmentLabels.add(build);
        });
        apartmentLabelService.saveBatch(apartmentLabels);

        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
        feeValueIds.forEach(feeValueId -> {
            ApartmentFeeValue build = ApartmentFeeValue.builder().apartmentId(apartmentSubmitVo.getId()).feeValueId(feeValueId).build();
            apartmentFeeValues.add(build);
        });
        apartmentFeeValueService.saveBatch(apartmentFeeValues);

        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        ArrayList<GraphInfo> graphInfos = new ArrayList<>();
        graphVoList.forEach(graphVo -> {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setItemId(apartmentSubmitVo.getId());
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setName(graphVo.getName());
            graphInfos.add(graphInfo);
        });
        graphInfoService.saveBatch(graphInfos);
    }

    @Override
    public void deleteApartmentById(Long id) {
        this.removeById(id);

        apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,id));
        apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,id));
        apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType,ItemType.APARTMENT).eq(GraphInfo::getItemId,id));

    }
}